﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Collections.ObjectModel;

namespace Yhdistelija
{
    public class FileJoiner : INotifyPropertyChanged
    {
        string sourcePath = "C:\\";
        List<string> sourceFiles = new List<string>();
        string targetPath = "C:\\";


        #region Properties

        public string SourcePath
        {
            get { return sourcePath; }
            set
            {
                sourcePath = value;
                OnPropertyChanged("SourcePath");
            }
        }

        public string TargetPath
        {
            get { return targetPath; }
            set
            {
                targetPath = value;
                OnPropertyChanged("TargetPath");
            }
        }

        public string[] SourceFiles
        {
            get { return sourceFiles.ToArray(); }
        }

        #endregion


        #region INotifyPropertyChanged interface

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }

        #endregion
        

        public void FindFiles()
        {
            sourceFiles.Clear();
            sourceFiles.AddRange(Directory.EnumerateFiles(sourcePath, "*.txt"));
            OnPropertyChanged("SourceFiles");
        }

        public void JoinFiles(IEnumerable<string> files)
        {
            File.WriteAllLines(targetPath, files.SelectMany(File.ReadLines));
        }
    }
}
